package com.example.sherlockzhong.plush_v2.Subscriptions;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.sherlockzhong.plush_v2.Components.buttons.BackButton;
import com.example.sherlockzhong.plush_v2.Constants.SystemConstant;
import com.example.sherlockzhong.plush_v2.R;
import com.example.sherlockzhong.plush_v2.Utils.NetworkUtils;

/**
 * Created by DosonLeung on 2014/10/17.
 */
public class PosterActivity extends Activity{
    BackButton back;
    WebView show = SystemConstant.show;
    ImageView refresh;
    ImageView background;
    final String signIn = "http://120.24.58.118/center/";
    final String signOut = "http://120.24.58.118/center/poster/signOut";
    private ProgressDialog pd;

    @Override
    public void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);
        setContentView(R.layout.poster_layout);
        initWidget();
    }

    private void initWidget(){
        String url;
        back = (BackButton)findViewById(R.id.poster_back);
        back.setApp(this);
        show = (WebView) findViewById(R.id.show);
        background = (ImageView) findViewById(R.id.hide);
        if(NetworkUtils.getAvailableNetWorkInfo(this)!=null) {
            background.setVisibility(View.GONE);
            if(SystemConstant.poster)
                url = signIn;
            else
                url = signOut;
            show.loadUrl(url);
            SystemConstant.poster = true;
        }else{
            background.setVisibility(View.VISIBLE);
            Toast.makeText(this,"网络超时，请检查您的网络设置",Toast.LENGTH_SHORT).show();
        }
        show.getSettings().setJavaScriptEnabled(true);
        show.setWebViewClient(new posterWebViewClient());
        refresh = (ImageView) findViewById(R.id.poster_refresh);
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshEvent();
            }
        });
    }

    //Web视图
    private class posterWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url,Bitmap favicon){
            if(NetworkUtils.getAvailableNetWorkInfo(PosterActivity.this)!=null) {
                background.setVisibility(View.GONE);
                super.onPageStarted(view, url,favicon);
                showReflashDialog("加载中");
            }else{
                background.setVisibility(View.VISIBLE);
                Toast.makeText(PosterActivity.this,"网络超时，请检查您的网络设置",Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            if(NetworkUtils.getAvailableNetWorkInfo(PosterActivity.this)!=null) {
                background.setVisibility(View.GONE);
                super.onPageFinished(view,url);
            }else{
                background.setVisibility(View.VISIBLE);
                Toast.makeText(PosterActivity.this,"网络超时，请检查您的网络设置",Toast.LENGTH_SHORT).show();
            }
            stopReflashDialog();
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    //刷新事件
    private void refreshEvent(){
        AnimationSet as = new AnimationSet(true);
        final float w = refresh.getWidth() / 2;
        final float h = refresh.getHeight() / 2;
        RotateAnimation ra = new RotateAnimation(0, 720, w, h);
        ra.setDuration(500);
        as.addAnimation(ra);
        refresh.startAnimation(as);
        show.reload();
    }

    /**
     * 显示刷新的时候的等待进度条
     */
    private void showReflashDialog(String msg) {
        this.pd = new ProgressDialog(this);
        this.pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        this.pd.setCancelable(true);
        this.pd.setMessage(msg);
        this.pd.show();
    }

    /**
     * 结束刷新的时候的等待进度条
     */
    private void stopReflashDialog() {
        if (this.pd != null) {
            pd.dismiss();
        }
    }

    public void onDestory() {
        super.onDestroy();
        stopReflashDialog();
    }
}
